<?php


namespace App\Http\Controllers\Mini\Circle;


use App\Http\Controllers\Controller;
use App\Models\FleaWallet;
use App\Models\FleaWithdrawal;
use \Illuminate\Http\Request;

/**
 * Class FleaWalletController
 * @package App\Http\Controllers\Mini\Circle
 * 邻里圈跳蚤市场钱包控制器
 */
class FleaWalletController extends Controller
{
    /**
     * 获取我的钱包信息
     */
    public function index()
    {
        try {
            $data = FleaWallet::getMyWalletData();
        } catch (\Exception $exception) {
            return $this->error($exception->getMessage());
        }
        return $this->success($data);
    }

    public function recordList(Request $request)
    {
        try {
            $where = [];
            $type = $request->get('type', 1);
            $where[] = ['type', '=', $type];

            $time = $request->get('time', date('Y-m'));

            $page = $request->get('page', 1);
            $limit = $request->get('limit', 20);

            $data = FleaWallet::recordList($where, $time, $page, $limit);
        } catch (\Exception $exception) {
            return $this->error($exception->getMessage());
        }
        return $this->success($data);
    }

    /**
     * 发起提现
     */
    public function store(Request $request)
    {
        try {
            $money = $request->post('money');
            FleaWallet::takeout($money);
        } catch (\Exception $exception) {
            return $this->error($exception->getMessage());
        }
        return $this->success();
    }

    /**
     * 查看提现记录
     */
    public function withdrawalList(Request $request)
    {
        try {
            $page = $request->get('page', 1);
            $limit = $request->get('limit', 20);

            $data = FleaWithdrawal::getWithdrawalList($page, $limit);
        } catch (\Exception $exception) {
            return $this->error($exception->getMessage());
        }

        return $this->success($data);
    }
}
